package com.jingdianjichi.subject.domain.service.impl;

import com.jingdianjichi.subject.common.enums.ISDeletedFlagEnum;
import com.jingdianjichi.subject.domain.convert.SubjectLabelBoConverter;
import com.jingdianjichi.subject.domain.entity.SubjectLabelBo;
import com.jingdianjichi.subject.domain.service.SubjectLabelDomainService;
import com.jingdianjichi.subject.infra.basic.entity.SubjectLabel;
import com.jingdianjichi.subject.infra.basic.entity.SubjectMapping;
import com.jingdianjichi.subject.infra.basic.service.SubjectLabelService;
import com.jingdianjichi.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {

    @Resource
    private SubjectLabelService subjectLabelService;

    @Resource
    private SubjectMappingService subjectMappingService;

    @Override
    public void add(SubjectLabelBo subjectLabelBo) {
        if(log.isInfoEnabled()){
            log.info("add----SubjectLabelDomainService:{}",subjectLabelBo);
        }
            subjectLabelBo.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
            SubjectLabel subjectLabel = SubjectLabelBoConverter.INSTANCE.converBoToEntity(subjectLabelBo);
            subjectLabelService.insert(subjectLabel);
    }

    @Override
    public void updateLabel(SubjectLabelBo subjectLabelBo) {
        if(log.isInfoEnabled()){
            log.info("updateLabel----SubjectLabelDomainService:{}",subjectLabelBo);
        }
        SubjectLabel subjectLabel = SubjectLabelBoConverter.INSTANCE.converBoToEntity(subjectLabelBo);
        subjectLabelService.update(subjectLabel);
    }

    @Override
    public void deleteLabel(SubjectLabelBo subjectLabelBo) {
        if(log.isInfoEnabled()){
            log.info("deleteLabel----SubjectLabelDomainService:{}",subjectLabelBo);
        }
        subjectLabelBo.setIsDeleted(ISDeletedFlagEnum.DELETED.getCode());
        SubjectLabel subjectLabel = SubjectLabelBoConverter.INSTANCE.converBoToEntity(subjectLabelBo);
        subjectLabelService.update(subjectLabel);
    }

    @Override
    public List<SubjectLabelBo> queryLabelByCategoryId(SubjectLabelBo subjectLabelBo) {
        Long categoryId = subjectLabelBo.getCategoryId();
        SubjectMapping subjectMapping=new SubjectMapping();
        subjectMapping.setCategoryId(categoryId);
        //设置查找未删除的字段
        subjectMapping.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
        List<SubjectMapping> mappingList=subjectMappingService.queryLabelId(subjectMapping);
        if (CollectionUtils.isEmpty(mappingList)){
            return Collections.emptyList();
        }
        List<Long> labelList = mappingList.stream().map(SubjectMapping::getLabelId)
                .collect(Collectors.toList());
        List<SubjectLabel> subjectLabelList=subjectLabelService.batchQueryById(labelList);

        List<SubjectLabelBo> boList=new LinkedList<>();
        subjectLabelList.forEach(label->{
            SubjectLabelBo bo=new SubjectLabelBo();
            bo.setId(label.getId());
            bo.setLabelName(label.getLabelName());
            bo.setSortNum(label.getSortNum());
            bo.setCategoryId(categoryId);
            boList.add(bo);
        });
        return boList;
//        myself
//        if(log.isInfoEnabled()){
//            log.info("queryLabelByCategoryId----SubjectLabelDomainService:{}",subjectLabelBo);
//        }
//        subjectLabelBo.setIsDeleted(ISDeletedFlagEnum.UN_DELETE.getCode());
//        SubjectLabel subjectLabel = SubjectLabelBoConverter.INSTANCE.
//                converBoToEntity(subjectLabelBo);
//
//        List<SubjectLabel> subjectLabelNews=subjectLabelService.queryLabelByCategoryId(subjectLabel);
//
//        List<SubjectLabelBo> subjectLabelBoList = SubjectLabelBoConverter.INSTANCE
//                .convertEntityListToBo(subjectLabelNews);
//
//        return subjectLabelBoList;
    }
}
